##===============================================================================
# # Copyright 2012 Jake Ross
# #
# # Licensed under the Apache License, Version 2.0 (the "License");
# # you may not use this file except in compliance with the License.
# # You may obtain a copy of the License at
# #
# #   http://www.apache.org/licenses/LICENSE-2.0
# #
# # Unless required by applicable law or agreed to in writing, software
# # distributed under the License is distributed on an "AS IS" BASIS,
# # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# # See the License for the specific language governing permissions and
# # limitations under the License.
##===============================================================================
#
##============= enthought library imports =======================
# from src.constants import NULL_STR
# from src.helpers.filetools import str_to_bool
##============= standard library imports ========================
##============= local library imports  ==========================
# from src.loggable import Loggable
# from src.regex import ALIQUOT_REGEX
#
# class RunParser(Loggable):
#    def _get_attr(self, attr):
#        if isinstance(attr, tuple):
#            attr, rattr = attr
#        else:
#            attr, rattr = attr, attr
#        return attr, rattr
#
#    def _get_idx(self, header, attr):
#        try:
#            return header.index(attr)
#        except ValueError:
#            pass
#
#    def _load_scripts(self, header, args):
#        script_info = dict()
#        # load scripts
#        for attr in [
#                     # ver. 1.0
#                     'measurement', 'extraction',
#                     'post_measurement',
#                     'post_equilibration',
#
#                     # ver. 2.0
#                     ('post_eq', 'post_equilibration'),
#                     ('post_meas', 'post_measurement'),
#                     ]:
#
#            attr, rattr = self._get_attr(attr)
#            idx = self._get_idx(header, attr)
#            if idx:
#                try:
#                    script_info[rattr] = args[idx]
#                except IndexError, e:
#                    self.debug('base schedule _run_parser {} {}'.format(e, attr))
#
#        return script_info
#
#    def parse(self, header, line, meta, delim='\t'):
#        params = dict()
#        if not isinstance(line, list):
#            line = line.split(delim)
#
#        args = map(str.strip, line)
#
#        script_info = self._load_scripts(header, args)
#
#        ln = args[header.index('labnumber')]
#        if ALIQUOT_REGEX.match(ln):
#            ln, a = ln.split('-')
#            params['aliquot'] = int(a)
#
#        params['labnumber'] = ln
#
#        # load strings
#        for attr in [
#                     'pattern',
#                     'position',
#                     'comment',
#
#                     # ver 1.0
#                     'extract_units',
#                     # ver 2.0
#                     ('e_units', 'extract_units')
#                     ]:
#
#            attr, rattr = self._get_attr(attr)
#            idx = self._get_idx(header, attr)
#            if idx:
#                try:
#                    params[rattr] = args[idx]
#                except IndexError, e:
#                    self.debug('base schedule _run_parser {} {}'.format(e, attr))
#
#        # load booleans
#        for attr in [
#                     # ver 1.0
#                     'autocenter',
#                     'disable_between_positions',
#
#                     # ver 2.0
#                     ('dis_btw_pos', 'disable_between_positions')
#                     ]:
#            attr = self._get_attr(attr)
#            idx = self._get_idx(header, attr)
#            if idx:
#                try:
#                    param = args[idx]
#                except IndexError:
#                    params[rattr] = False
#                    continue
#
#                if param.strip():
#                    bo = str_to_bool(param)
#                    if bo is not None:
#                        params[rattr] = bo
#                    else:
#                        params[rattr] = False
#
#        # load numbers
#        for attr in ['duration', 'overlap', 'cleanup',
# #                     'aliquot',
#                     'extract_group',
#                     'weight',
#                     # ver 1.0
#                     'extract_value',
#                     # ver 2.0
#                     ('e_value', 'extract_value'),
#                     ]:
# #            if isinstance(attr, tuple):
# #                attr, rattr = attr
# #            else:
# #                attr, rattr = attr, attr
#            attr, rattr = self._get_attr(attr)
#            idx = self._get_idx(header, attr)
#            if idx:
#                try:
#                    param = args[idx]
#                    params[rattr] = float(param.strip())
#                except (IndexError, ValueError), e:
#                    print e
#                    pass
#
# #            try:
# #                idx = header.index(attr)
# #            except ValueError, e:
# #                print e
# #                continue
# #                try:
# #                    param = args[idx]
# #                    params[rattr] = float(param.strip())
# #                except (IndexError, ValueError), e:
# #                    print e
# #                    pass
# #            try:
# #                param = args[header.index(attr)].strip()
# #                if param:
# #                    params[attr] = float(param)
# #            except (IndexError, ValueError):
# #                pass
#
#        # default extract_units to watts
# #        print header.index('extract_value'), len(args)
# #        for attr in [
# #                     # ver 1.0
# #                     'extract_value',
# #                     'extract_units',
# #                     # ver 2.0
# #                     ('e_value', 'extract_value'),
# #                     ('e_units', 'extract_units')
# #                     ]:
# #            if isinstance(attr, tuple):
# #                attr, rattr = attr
# #            else:
# #                attr, rattr = attr, attr
# #
# #            try:
# #                idx = header.index(attr)
# #            except ValueError:
# #                continue
# #
# #            try:
# #                param = args[idx]
# #                params[rattr] = param
# #            except IndexError:
# #                pass
#
# #        extract_value = args[header.index('extract_value')]
# #        extract_units = args[header.index('extract_units')]
# #        if not extract_units:
# #            extract_units = '---'
# #
# #        params['extract_value'] = extract_value
# #        params['extract_units'] = extract_units
#
# #        def make_script_name(n):
# #            try:
# #                na = args[header.index(n)]
# #                if na.startswith('_'):
# #                    if meta:
# #                        na = meta['mass_spectrometer'] + na
# #
# #                if na and not na.endswith('.py'):
# #                    na = na + '.py'
# #            except IndexError, e:
# #                print 'base schedule make_script_name ', e
# #                na = NULL_STR
# #
# #            return na
# #        for k, pa in params.iteritems():
# #            print k, pa
#
#        return script_info, params  # , make_script_name
#
#
# class UVRunParser(RunParser):
#    def parse(self, header, line, meta, delim='\t'):
#        params = super(UVRunParser, self).parse(header, line, meta, delim)
#        if not isinstance(line, list):
#            line = line.split(delim)
#
#        args = map(str.strip, line)
#
#        def _set(attr, cast):
#            try:
#                v = args[header.index(attr)]
#                params[attr] = cast(v)
#            except (IndexError, ValueError, TypeError):
#                pass
#
#        _set('reprate', int)
#        _set('attenuator', str)
#        _set('mask', str)
#        _set('image', str)
#
#        return params
##============= EOF =============================================
